<?php

    session_start();
    require_once("../includes/conn.php");
    require_once "../includes/wxtoken.php";

    $sysconfig=$dbconn->fetch($dbconn->query("select * from ".DBQIAN."_sys_config where uid=1"));


    define('APPID',$sysconfig['cappid']);
    define('APPSECRET',$sysconfig['cappsecret']);
    $tools=new tools();
    if(!$tools->check_is_weixin())
    {
        echo '
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>入口不合法</title>
        </head>

        <body>
         错误：必须在微信环境下进入。
        </body>
        </html>
        ';
		exit;
    }

    $wxtoken = new wxtoken(1);

    $signPackage = $wxtoken->GetSignPackage();

   $ucode= $_SESSION['ucode']?$_SESSION['ucode']:'';
   if(isset($_GET['code']) &&$_GET['code']!=''&&$ucode=='')
    {
        $WXCODE=$_GET['code'];
        $getucodeurl="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".APPID."&secret=".APPSECRET."&code=".$WXCODE."&grant_type=authorization_code";
        $getucodejson=json_decode($tools->http_curl_get($getucodeurl,true));
        $wxlintoken=$getucodejson->access_token;
        $ucode=$getucodejson->openid;
        $getutxturl="https://api.weixin.qq.com/sns/userinfo?access_token=".$wxlintoken."&openid=".$ucode."&lang=zh_CN";
        $getutxtjson =json_decode($tools->http_curl_get($getutxturl,true));
        $uickname    =$getutxtjson->nickname;
        $usex        =intval($getutxtjson->sex);
        $headimgurl  =$getutxtjson->headimgurl;
        $nums=$dbconn->countn(DBQIAN."_user_list"," ucode='$ucode' and uid=1 ");
        if($nums==0){
            $times=time();
            $dbconn->noretquery("insert into ".DBQIAN."_user_list(ucode,uickname,usex,uheadimgurl,utime,uid)values
                     ('$ucode','$uickname',$usex,'$headimgurl',$times,1)");
        }

   }   
    elseif($ucode=='')
     {
         header("Location:top_index.php");exit();

     }
    $_SESSION['ucode']=$ucode;


    $data=$dbconn->fetch($dbconn->query("select * from ".DBQIAN."_score where ucode='".$ucode."'"));
    $score=intval($data['score'])?intval($data['score']):0;
	$score_plus=intval($data['score_plus'])?intval($data['score_plus']):0;


	$query=$dbconn->query("select s.*,u.uickname from ".DBQIAN."_score  as s left join ".DBQIAN."_user_list as u on u.ucode=s.ucode and u.ucode!='' order by (score+score_plus) desc,update_time asc limit 100");
    


    $i_ord=1;
    $i=0;
    $my_position=0;


   while($row=$dbconn->fetch($query)){

    	$data_user[$i]['uname']=$row['uickname'];
    	$position=$i_ord;
    	if($i_ord<10)
        {$position='0'.$i_ord;}

    	$data_user[$i]['position']=$position;
    	$data_user[$i]['score']=$row['score']+$row['score_plus'];

    	if($data['ucode']==$row['ucode'])
    	{
    		$my_position=$i_ord;
    	}
    	$i_ord++;
    	$i++;
    }


    // header("Location:mindex.html");exit;
  $utitle="我一口气接了".$score."个月饼，不来挑战，大奖就是我的啦!";
  $utitle_ct="中秋送礼，现金大奖等你拿";
    echo '
		<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
	<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	 <meta name="viewport" content="width=device-width,maximum-scale=1.0,user-scalable=no"/>
	<title>接月饼</title>
	<script src="lib/jquery-1.11.0.min.js"></script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
    <script>
      wx.config({   
        debug: false,
        appId: \'';echo $signPackage["appId"];;echo '\',
        timestamp: ';echo $signPackage["timestamp"];;echo ',
        nonceStr: \'';echo $signPackage["nonceStr"];;echo '\',
        signature: \'';echo $signPackage["signature"];;echo '\',
        jsApiList: [
            \'checkJsApi\',
            \'onMenuShareTimeline\',
            \'onMenuShareAppMessage\',
            \'onMenuShareQQ\',
            \'onMenuShareWeibo\',
            \'hideMenuItems\',
            \'showMenuItems\',
            \'hideAllNonBaseMenuItem\',
            \'showAllNonBaseMenuItem\',
            \'translateVoice\',
            \'startRecord\',
            \'stopRecord\',
            \'onRecordEnd\',
            \'playVoice\',
            \'pauseVoice\',
            \'stopVoice\',
            \'uploadVoice\',
            \'downloadVoice\',
            \'chooseImage\',
            \'previewImage\',
            \'uploadImage\',
            \'downloadImage\',
            \'getNetworkType\',
            \'openLocation\',
            \'getLocation\',
            \'hideOptionMenu\',
            \'showOptionMenu\',
            \'closeWindow\',
            \'scanQRCode\',
            \'chooseWXPay\',
            \'openProductSpecificView\',
            \'addCard\',
            \'chooseCard\',
            \'openCard\'
        ]
      });
      wx.checkJsApi({
          jsApiList: [
            \'getNetworkType\',
            \'previewImage\'
          ],
          success: function (res) {
            alert(JSON.stringify(res));
          }
      });
      wx.ready(function () {
        wx.onMenuShareTimeline({
             title: "';echo $utitle;;echo '",
             link: "';echo WEBNAME."main/index.php?utocode=".$ucode;;echo '",
             imgUrl: "';
    echo WEBNAME."main/images/order_gx.jpg";;echo '",
             trigger: function (res) {
             },
             success: function (res) {       
             },
             cancel: function (res) {            
             },
             fail: function (res) {
             }
        });
        wx.onMenuShareAppMessage({
            title: "';echo  $utitle;;echo '",
            desc: "';echo  $utitle_ct;;echo '",
            link: "';echo WEBNAME."main/index.php?utocode=".$ucode;;echo '",
            imgUrl: "';
    echo WEBNAME."main/images/order_gx.jpg";;echo '",
            trigger: function (res) {
            },
            success: function (res) {
            },
            cancel: function (res) {
            },
            fail: function (res) {
            }
         });
    });
    </script>

	
	<style>
	body{margin:0px; padding:0px; font-size:16px; font-family:"Microsoft YaHei" ! important;}
	ul{ list-style:none;}
	.ucontent{ width:100%;}
	.div_c1{ clear:both;width:90%;padding:0px 15px; margin:0 auto;text-align:center; }
	.div_c2{ width:90%;  margin:0 auto; text-align:center;/* background:url(images/order_position.png) no-repeat; background-size:100% 100%;*/}
	.div_c2 .font1{color:#773528; font-weight:600;position:absolute;z-index:3;right:20%;padding-top:6%;}
	.div_c2 .font2{color:#773528;font-weight:600;position:absolute;z-index:3;right:20%;padding-top:17.1%;}
	
	.div_jg{ width:90%; margin:0 auto;height:10px; line-height:10px; position:relative !important;}
	
	.div_c3{width:90%;margin:0 auto;text-align:center; }
	.div_c3 .tx1{position:absolute;z-index:2;padding-left:5%;padding-right:9%; padding-top:40px; padding-bottom:20px; font-size:14px;}
	
	
	</style>
	</head>
	
	<body>
	<div id="Layer1" style="position:fixed; width:100%; height:100%; z-index:-2">    
	<img src="images/order_background.png" height="100%" width="100%"/>    
	</div>
	
	<div class="ucontent">
		<div class="div_c1">
		<img src="images/order_top.png" width="100%"  height="100%"/>
		</div>
		<div class="div_c2">
		<img src="images/order_position.png" width="90%" style="position:absolute;z-index:2;left:5%;"/>
		 <div class="font1">'. $score."+". $score_plus.'个</div>
		 <div class="font2">'.$my_position.'名</div> 
		</div>
		<div class="div_jg"></div>
		 <img src="images/order_moon.png" width="20%" style="position:absolute;z-index:1;left:0.1%;margin-top:22%;"/>
		<div class="div_c1" style="margin-top:28%;padding 20px 0px;" >
		<a href="index.php"><img src="images/order_btinto3.png" width="80%"   style="position:absolute;z-index:2;left:10%;"/></a>
		</div>
		
		 <div class="div_c3" style="padding-top:16%;">
		 <img src="images/order_tlback.png" width="90%" height="2680px;"  style="position:absolute;z-index:1;left:5%;"/>
		  <div class="tx1">
	
			   <table cellpadding="0" border="0" cellspacing="0" >
			   <tr ><td colspan="3" style="padding-bottom:5px;"><img src="images/order_title.png" width="100%" /></td></tr>
			   ';
			   foreach ($data_user as $key => $value) {
				   echo '
				   <tr>
				   <td style="width:40%;border-bottom:1px solid #650D01; padding:2px 0px;">'.$value['uname'].'</td>
				   <td style="width:30%;border-bottom:1px solid #650D01;padding:2px 0px;">'.$value['position'].'</td>
				   <td style="width:30%;border-bottom:1px solid #650D01;padding:2px 0px;">'.$value['score'].'</td>
				   </tr>';

			   }


			   echo '
			   </table>
			</div>
		</div>
		
		<div style="margin-top:2700px;"></div>
	
	
	</div>
	</body>
	</html>

    ';

	
?>